#include "RF_functions.h"
//#define two gsl_gsl_complex(2.0,0.0)
gsl_complex Gamma2Z(gsl_complex a, double Z0)
{
        gsl_complex Impd50 = gsl_complex_rect(Z0,0);
        //gsl_complex one = gsl_complex_rect(1.0,0.0);
        gsl_complex temp = gsl_complex_mul(Impd50,gsl_complex_div(gsl_complex_add(one,a),gsl_complex_add(one,gsl_complex_negative(a))));
        return temp;
}


gsl_complex Renorm_Gamma(gsl_complex a, float Z0, float NewZ0)
{
        gsl_complex Z = Gamma2Z( a, Z0);
        gsl_complex temp = Z2Gamma( Z, NewZ0);
        return temp;
}

gsl_complex Z2Gamma(gsl_complex a, float Z0)
{
        gsl_complex Impd50 = gsl_complex_rect(Z0,0);
        gsl_complex temp = gsl_complex_div(gsl_complex_sub(a,Impd50),gsl_complex_add(a,Impd50));
        return temp;
}

gsl_complex Z2Y(gsl_complex a)
{
        gsl_complex temp = gsl_complex_div(gsl_complex_conjugate(a),gsl_complex_mul(a,gsl_complex_conjugate(a)));
        return temp;
}


float linear_interpolation(QList<float> X,  QList<float> Y, float val)
{
    double res,x[X.count()], y[Y.count()];

    QString temp;
    int i;

    {
        gsl_interp_accel *acc = gsl_interp_accel_alloc ();
        gsl_spline *spline = gsl_spline_alloc ( gsl_interp_linear, X.count());//gsl_interp_cspline gsl_interp_linear
        for (i = 0; i < X.count(); i++)
        {
            x[i] = X.at(i);
            y[i] = Y.at(i);

        }
        gsl_spline_init (spline, x, y, X.count());

        i=0;
        for (float j = X.first(); j < X.last(); j+=(X.last()-X.first())/20)
        {
            res = gsl_spline_eval (spline, j, acc);
            i++;
        }
        res = gsl_spline_eval (spline, val, acc);

        gsl_spline_free (spline);
        gsl_interp_accel_free (acc);
    }

    return res;
}


